// This file is part of OpenMeca, an easy software to do mechanical simulation.
//
// Author(s)    :  - Damien ANDRE  <openmeca@gmail.com>
//
// Copyright (C) 2012 Damien ANDRE
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.


#include <cassert>
#include <QSpacerItem>
#include <QMessageBox>

#include "OpenMeca/Gui/UserItemDialogLinkBase.hpp"
#include "OpenMeca/Item/LinkBase.hpp"
#include "OpenMeca/Item/Body.hpp"

namespace OpenMeca
{
  namespace Gui
  {
  
    UserItemDialogLinkBase::UserItemDialogLinkBase(QWidget* parent)
      :id_(new WidgetString(parent)),
       body1_(new WidgetSelectItem<Core::AutoRegisteredPtr<Item::Body, Item::LinkBase> > (parent)),
       body2_(new WidgetSelectItem<Core::AutoRegisteredPtr<Item::Body, Item::LinkBase> > (parent)),
       parent_(parent),
       point_(new WidgetPoint(parent)),
       quaternion_(new WidgetQuaternion(parent))

    { 
      id_->SetLabel(QObject::tr("Name"));
      body1_->SetLabel(QObject::tr("First body"));
      body2_->SetLabel(QObject::tr("Second body"));

      QVBoxLayout* layout = new QVBoxLayout(parent);
      layout->addWidget(id_);
      layout->addWidget(body1_);
      layout->addWidget(body2_);
      layout->addWidget(point_);
      layout->addWidget(quaternion_);
      point_->SetLabel(QObject::tr("Center"));
      point_->SetDimension(Util::Dimension::Get("Length"));
      quaternion_->SetLabel(QObject::tr("Rotation"));
    }
    
    UserItemDialogLinkBase::~UserItemDialogLinkBase()
    {
    }

    void 
    UserItemDialogLinkBase::Init()
    {
      id_->SetValue(GetCurrentLink().GetName());
      GetWidgetSelectItemBody1().SetValue(GetCurrentLink().GetBody1Ptr());
      GetWidgetSelectItemBody2().SetValue(GetCurrentLink().GetBody2Ptr());
      point_->SetValue(GetCurrentLink().GetCenter());
      quaternion_->SetValue(GetCurrentLink().GetQuaternion());
    }
    

    bool
    UserItemDialogLinkBase::Check()
    {
      if (body1_->GetComboBox().currentIndex() == body2_->GetComboBox().currentIndex())
	{
	  QMessageBox::warning(parent_, QObject::tr("Warning"), QObject::tr("The bodys must be different"));
	  return false;
	}
      return true;
    }

    WidgetSelectItem<Core::AutoRegisteredPtr<Item::Body, Item::LinkBase> >& 
    UserItemDialogLinkBase::GetWidgetSelectItemBody1()
    {
      return * body1_;
    }
    
    WidgetSelectItem<Core::AutoRegisteredPtr<Item::Body, Item::LinkBase> >&
    UserItemDialogLinkBase::GetWidgetSelectItemBody2()
    {
      return * body2_;
    }

  
  }
}
